package hk;

import javax.swing.*;
import java.awt.*;

public class Chess extends JFrame implements CS{
    int i=2;
    private Qizi qizis[][]=new Qizi[line][line];//用qizi类型的二维数组来存储棋子
    private JButton btnRegretChess = new JButton("悔棋");
    private JButton btnRestart =new JButton("重新开始");
    private JLabel jLabel=new JLabel("请选择对战模式");
    private JLabel jLabel2=new JLabel("请选择棋子颜色");
    private int modeValue;//提示框选项的值
    private int colorValue;
    Dimension dimension=new Dimension(100, 30);


    public void chessUI() {
        btnRegretChess.setPreferredSize(dimension);
        btnRestart.setPreferredSize(dimension);
        this.setSize(700, 700);
        this.setLayout(new FlowLayout());
        this.setTitle("五子棋");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(3);//JFrame.EXIT_ON_CLOSE=3
        this.setVisible(true);
        this.add(btnRegretChess);
        this.add(btnRestart);
        modeValue =JOptionPane.showOptionDialog(this, jLabel, "提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
                null, modes, null);
        colorValue =JOptionPane.showOptionDialog(this, jLabel2, "提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
                null, colors, null);
        //关闭提示框则退出程序
        if(modeValue ==JOptionPane.CLOSED_OPTION|| colorValue ==JOptionPane.CLOSED_OPTION) {
            System.exit(1);
        }


        Graphics g=this.getGraphics();
        MousListener mouslistener =new MousListener(g, qizis,this, modeValue, colorValue);
        this.addMouseListener(mouslistener);//窗口添加监听
        btnRegretChess.addActionListener(mouslistener);//按钮添加监听
        btnRestart.addActionListener(mouslistener);

    }

    //绘制
    @Override
    public void paint(Graphics g) {//重绘棋盘和棋子
        super.paint(g);
        drawChess(g);
        //绘制悔棋
        drawQZ();
    }
    //画一个棋盘
    public void drawChess(Graphics g) {
        g.setColor(Color.black);
        //绘制横线
        for(int i=0;i<line;i++) {
            g.drawLine(x0, y0+size*i, x0+(line-1)*size, y0+size*i);
        }
        //绘制竖线
        for(int j=0;j<line;j++) {
            g.drawLine(x0+size*j, y0, x0+size*j, y0+(line-1)*size);
        }
    }
    //重构画棋子
    public void drawQZ() {
        for(int ix=0;ix<line;ix++) {
            for(int iy=0;iy<line;iy++) {
                if(qizis[ix][iy]!=null) {
                    Qizi qizi21= qizis[ix][iy];
                    qizi21.drawq();
                }
            }
        }
    }

}
